<?php

namespace App\Admin\Controllers\AdminUser;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Models\AdminUser;
use App\Admin\Models\AdminUserRole;
use App\Admin\Services\AdminMenuRole;
use App\Admin\Validate\AdminUser\AdminUser as ValidateAdminUserAdminUser;
use App\Common\Services\Admin\Common;
use Illuminate\Support\Facades\Redis;

class AdminUserController extends Controller
{

    /**
     * 获取用户详情
     */
    public function getinfo(Request $request)
    {
        $info = Common::getCurrentAdmininfo($request);
        $info = AdminUser::where(['id' => $info['id']])->first();
        $info['account_name']=$info['username'];

        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 编辑个人信息
     */
    public function editinfo(Request $request)
    {
        $info = Common::getCurrentAdmininfo($request);
        $req_data = $request->only(['real_name', 'user_img', 'email','salary', 'mobile']);
        $data=['update_at'=>time()];
        foreach($req_data as $key=>$val){
            if($val!=''){
                $data[$key]=$val;
            }
        }
        AdminUser::where(['id' => $info['id']])->update($data);



        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }



    /**
     * 获取运维账号列表
     */
    public function getoplist(Request $request)
    {
        $list = AdminUser::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取账户管理详情
     */
    public function getopinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $info = AdminUser::where(['id' => $id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 新增/编辑运维账号
     */
    public function dopost(Request $request)
    {
        $req_data = $request->only(['id', 'user_no', 'real_name', 'username', 'mobile']);

        $Validate = new ValidateAdminUserAdminUser;
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        if (isset($req_data['id']) && intval($req_data['id']) > 0) {
            $isexist = AdminUser::where([['username', $req_data['username']], ['id', '<>', $req_data['id']]])
                // ->orWhere([['mobile', $req_data['mobile']],['id','<>',$req_data['id']]])
                ->first();
        } else {
            $isexist = AdminUser::where([['username', $req_data['username']]])
                // ->orWhere([['mobile', $req_data['mobile']]])
                ->first();
        }


        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '用户名、手机号不允许重复']);
        }

        //判断账套信息是否包含超管信息

        $isexist = AdminUser::where([['username', $req_data['username']], ['type', 1]])->first();

        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '运维账号信息不可与超管信息相同']);
        }

        $data = [
            'user_no' => isset($req_data['user_no']) ? $req_data['user_no'] : '',
            'real_name' => $req_data['real_name'],
            'username' => $req_data['username'],
            'mobile' => $req_data['mobile'],
            'type' => 2,
            'update_at' => time()
        ];
        if (isset($req_data['id']) && intval($req_data['id']) > 0) {
            AdminUser::where(['id' => $req_data['id']])->update($data);
            //编辑完账号是否需要重新登录，能否编辑账号信息
            $msg = '编辑运维账号成功';
            Common::cleartokenkey($req_data['id']);

        } else {
            $data['create_at'] = time();
            $data['password'] = MD5(MD5('zhuxiaobao_' . env('ADMIN_DEF_PWD')));
            $admin_id=AdminUser::create($data)->id;
            $msg = '新增运维账号成功';
            //设置权限
            AdminMenuRole::setAdminRole($admin_id,2);
        }

        return response()->json(['code' => '000', 'msg' => $msg]);
    }


    /**
     * 冻结
     */
    public function changestatus(Request $request)
    {
        $id = $request->id ?? 0;
        $info = AdminUser::where([['id', $id]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $status = $info->status == 1 ? 2 : 1;
        AdminUser::where([['id', $id]])->update(['status' => $status, 'update_at' => time()]);

        //冻结需删除管理员以及子账号登录状态
        $status == 2 && Common::clearTokenKey($id);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 重置密码
     */
    public function resetpassword(Request $request)
    {
        //重置后需重置账号登陆状态
        $id = $request->id ?? 0;
        $info = AdminUser::where([['id', $id]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $password = MD5(MD5('zhuxiaobao_' . env('ADMIN_DEF_PWD')));
        AdminUser::where([['id', $id]])->update(['password' => $password, 'update_at' => time()]);

        //重置后需重置账号登陆状态
        Common::cleartokenkey($id);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }



    /**
     * 获取权限
     */

     public function getruleinfo(Request $request)
     {
         //获取菜单列表以及管理员已设置的权限
         $id = $request->id ?? 0;
         $info = AdminUser::where([['id', $id],['type',2]])->first();
         if (!$info) {
             return response()->json(['code' => '001', 'msg' => '参数错误']);
         }
         $rule_info = AdminUserRole::where(['admin_id' => $id])->first();
         $ruleinfo = json_decode($rule_info->ruleinfo, true);
         return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $ruleinfo]);
     }

     /**
      * 设置权限
      */
     public function dopostrule(Request $request)
     {

         //设置权限后，需要对比用户现有权限
         $id = $request->id ?? 0;
         $ruleinfo = is_array($request->ruleinfo) ? $request->ruleinfo : (is_array(json_decode($request->ruleinfo, true)) ? json_decode($request->ruleinfo, true) : []);
         $info = AdminUser::where([['id', $id],['type',2]])->first();
         if (!$info) {
             return response()->json(['code' => '001', 'msg' => '参数错误']);
         }
         if (count($ruleinfo) <= 0) {
             return response()->json(['code' => '001', 'msg' => '权限菜单信息有误']);
         }
         $rule_info = json_encode($ruleinfo);
         AdminUserRole::where(['admin_id' => $id])->update(['ruleinfo' => $rule_info]);
         Redis::set('admin_user_menulist_Admin_' . $id, $rule_info);

         //重置用户现有权限
         return response()->json(['code' => '000', 'msg' => '操作成功']);
     }
}
